from os import makedirs

import requests
import re
from bs4 import BeautifulSoup
from contextlib import closing

from os.path import exists
import sys

sys.setrecursionlimit(1000000)


def req_get_bf(url):
    return BeautifulSoup(requests.get(url, timeout=180).text, 'html.parser')


def just_url(temp_url, detail_name):

    temp_bf = req_get_bf(temp_url)
    useragents = temp_bf.find_all('td', 'useragent')
    if useragents:
        for agent in useragents:
            for str in agent.find_all('a'):
                string = str.string
                if string.startswith("Mozilla"):
                    print(string)
                    with open('IPoneUseragent.txt', 'a') as file_object:  # windows 系统中 ，路径间隔符号是反斜杠 \
                        file_object.write(string)
                        file_object.write("\n")


if __name__ == '__main__':
        for i in range(1, 12):
            html = "https://developers.whatismybrowser.com/useragents/explore/operating_platform/iphone/" + str(i)
            just_url(html, 'home')
